<?php
namespace data\set\ad;
use \data\BaseData;
/**
 * 广告
 */
class AdData extends BaseData{
    
    /**
     * 读取一个管理员信息
     */
    public function find($param){
        $check = ['id','plat','tag'];
        $where = $this->parseWhere($check,$param);
        if( !$where )  dataE('查询条件不能为空');
        $info  = M('set_advert')->where($where)->find();
        return $info;
    }
    
    /**
     * 添加一个 广告位
     */
    public function add($data){               
        $check = [            
            'siteid'    => ['reg'=>'int'],
            'status'    => ['val'=>1],
            'sort'      => ['def'=> 50,'reg'=>'int'],
            'positionid'=> ['check'=>'must','msg'=> '广告位参数不能为空','reg'=>'int'],
            'tag'       => ['check'=>'must','msg'=> '广告位标识不能为空'],
            'pic'       => ['check'=>'must','msg'=> '请上传广告图片'],
            'name'      => '',
            'des'       => '',            
            'url'       => '',
            'starttime' => ['reg'=>'int'],
            'endtime'   => ['reg'=>'int'],
            'addtime'   => ['val'=>SYS_TIME],
            'edittime'  => ['val'=>0]
        ];
        $save = $this->verifyData($check,$data);
        $id = M('set_advert')->insertGetId($save);
        if( !$id )  dataE('添加广告失败');
        return $id;
    }
    
    /**
     * 编辑一个 广告位
     */
    public function edit($id,$data){        
        if( !$id ) dataE('修改参数不能为空');
        $check = [            
            'status'    => ['reg'=>[1,2]],
            'sort'      => ['reg'=>'int'],
            //'positionid'=> ['reg'=>'int'],
            //'tag'       => ['reg'=>'must'],
            'pic'       => ['reg'=>'must'],
            'name'      => '',
            'des'       => '',            
            'url'       => '',
            'starttime' => ['reg'=>'int'],
            'endtime'   => ['reg'=>'int'],
            'edittime'  => ['val'=>SYS_TIME]
        ];    
        $save = $this->verifyData($check,$data);
        if( !$save ) return false;
        $where = ['id'=>$id];
        $result = M('set_advert')->where($where)->update($save);
        if($result === false ) dataE('修改广告信息失败');
        return $result;
    }
    
    /**
     * 列表读取
     */
    public function getList($param){
        $page = $param['page']?:1;
        $limit= $param['limit']?:20;
        $check = ['tag','positionid','siteid','status','starttime'=>'gt','endtime'=>'lt'];
        $where = $this->parseWhere($check,$param);
        $list  = M('set_advert')->where($where)->limit($limit)->page($page)->order('sort desc,id desc')->select();
        $total = M('set_advert')->where($where)->count();
        return [            
            'page' => $page,
            'limit' => $limit,
            'total'=> $total,
            'list' => $list,
        ];
    }
}